package org.ragdollcat.first.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.ragdollcat.first.common.BaseResponse;
import org.ragdollcat.first.common.DeleteRequest;
import org.ragdollcat.first.common.ResultUtils;
import org.ragdollcat.first.constants.UserConstant;
import org.ragdollcat.first.exception.ErrorCode;
import org.ragdollcat.first.exception.ThrowUtils;
import org.ragdollcat.first.model.annotation.AuthCheck;
import org.ragdollcat.first.model.dto.user.*;
import org.ragdollcat.first.model.entity.User;
import org.ragdollcat.first.model.enums.UserRoleEnum;
import org.ragdollcat.first.model.vo.LoginUserVO;
import org.ragdollcat.first.model.vo.UserVO;
import org.ragdollcat.first.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public BaseResponse<?> userRegister(@RequestBody UserRegisterRequestDTO registerRequestDTO) {
        ThrowUtils.throwIf(registerRequestDTO == null, ErrorCode.PARAMS_ERROR, "请求参数为空");
        String account = registerRequestDTO.getAccount();
        String password = registerRequestDTO.getPassword();
        String rePassword = registerRequestDTO.getRePassword();
        return userService.userRegister(account, password, rePassword);
    }

    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequestDTO userLoginRequestDTO, HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(userLoginRequestDTO == null, ErrorCode.PARAMS_ERROR, "请求参数为空");
        return userService.userLogin(userLoginRequestDTO.getUserAccount(), userLoginRequestDTO.getUserPassword(), httpServletRequest);
    }

    @GetMapping("/user/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest httpServletRequest) {
        User loginUser = userService.getLoginUser(httpServletRequest);
        return ResultUtils.success(userService.castUserToLoginUserVO(loginUser));
    }

    @PostMapping("/logout")
    public BaseResponse<?> loginout(HttpServletRequest httpServletRequest) {
        userService.userLoginout(httpServletRequest);
        return ResultUtils.success(null);
    }

    /**
     * 管理员添加用户
     * @param userAddRequest
     * @return
     */
    @PostMapping("/addUser")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> addUser(@RequestBody UserAddRequest userAddRequest){
        ThrowUtils.throwIf(ObjectUtils.isEmpty(userAddRequest),ErrorCode.PARAMS_ERROR,"参数为空！");
        User user = new User();
        final String password = "123456";
        String encryptPassword = userService.passwordEncryption(password);
        user.setUserPassword(encryptPassword);
        BeanUtils.copyProperties(userAddRequest,user);
        return ResultUtils.success(userService.save(user));
    }

    /**
     * 管理员删除用户
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delUser")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> delUser(@RequestBody DeleteRequest deleteRequest){
        ThrowUtils.throwIf(ObjectUtils.isEmpty(deleteRequest),ErrorCode.PARAMS_ERROR,"参数为空！");
        return ResultUtils.success(userService.removeById(deleteRequest.getId()));
    }

    /**
     * 管理员更新用户
     * @param userUpdateRequest
     * @return
     */
    @PostMapping("/updUser")
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updUser(@RequestBody UserUpdateRequest userUpdateRequest){
        ThrowUtils.throwIf(ObjectUtils.isEmpty(userUpdateRequest),ErrorCode.PARAMS_ERROR,"参数为空！");
        User user = new User();
        BeanUtils.copyProperties(userUpdateRequest,user);
        return ResultUtils.success(userService.updateById(user));
    }

    /**
     * 管理员根据ID获取未脱敏用户
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(Long id){
        ThrowUtils.throwIf(ObjectUtils.isEmpty(id),ErrorCode.PARAMS_ERROR,"参数为空！");
        User user = userService.getById(id);
        ThrowUtils.throwIf(ObjectUtils.isEmpty(user),ErrorCode.NOT_FOUND_ERROR,"用户不存在！");
        return ResultUtils.success(user);
    }

    /**
     * 用户根据ID获取脱敏用户
     * @param id
     * @return
     */
    @GetMapping("/getUserVoById")
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<UserVO> getUserVoById(Long id){
        ThrowUtils.throwIf(ObjectUtils.isEmpty(id),ErrorCode.PARAMS_ERROR,"参数为空！");
        User user = userService.getById(id);
        ThrowUtils.throwIf(ObjectUtils.isEmpty(user),ErrorCode.NOT_FOUND_ERROR,"用户不存在！");
        UserVO userVO = userService.castUserToUserVo(user);
        return ResultUtils.success(userVO);
    }


    /**
     * 管理员分页获取用户列表
     * @param userQueryRequest
     * @return
     */
    @PostMapping("/getUserPage")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> getUserPage(@RequestBody UserQueryRequest userQueryRequest){
        ThrowUtils.throwIf(ObjectUtils.isEmpty(userQueryRequest),ErrorCode.PARAMS_ERROR,"参数为空！");
        //首先分页条件查询结果
        long current = userQueryRequest.getCurrent();
        long pageSize = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<User>(current, pageSize), userService.getUserQueryWrapper(userQueryRequest));
        //脱敏
        List<UserVO> userVOS = userService.castUsersToUserVos(userPage.getRecords());
        Page<UserVO> userVOPage = new Page<>(current,pageSize,userPage.getTotal());
        return ResultUtils.success(userVOPage.setRecords(userVOS));
    }

}
